package com.zyy.transaction.demo.service.impl;

import com.zyy.transaction.demo.dao.AccountMapper;
import com.zyy.transaction.demo.pojo.Account;
import com.zyy.transaction.demo.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhangyiying
 * @ClassName: TransferServiceImpl
 * @Description:
 * @date 2022/2/24 17:30
 */
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public void substitute(String fromCardNo, String toCardNo, int money) {
        this.transfer(fromCardNo, toCardNo, money);
    }

    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) {
        Account fromAccount = accountMapper.findByCardNo(fromCardNo);
        Account toAccount = accountMapper.findByCardNo(toCardNo);
        if (fromAccount.getMoney() >= money){
            fromAccount.setMoney(fromAccount.getMoney() - money);
            accountMapper.updateAccountByCardNo(fromAccount);
            toAccount.setMoney(toAccount.getMoney() + money);
            int i = 100 / 0;
            accountMapper.updateAccountByCardNo(toAccount);
        }
    }

}
